package com.demo.dropdataack.service.impl;


import com.demo.dropdataack.dao.TblMapper;
import com.demo.dropdataack.entity.BpmRequest;
import com.demo.dropdataack.entity.Message;
import com.demo.dropdataack.entity.tblBpmEventsFromHangYiXing;
import com.demo.dropdataack.service.TblService;
import com.demo.dropdataack.service.common.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;


@Service
public class TblServiceImpl implements TblService {
    @Autowired
    private TblMapper tblMapper;

    @Autowired
    private TokenHelper tokenHelper;
    @Autowired
    private PushDataHelper pushDataHelper;

    @Autowired
    private PushDataPre pushDataPre;

    @Autowired
    private DropDataHelper dropDataHelper;

    @Autowired
    private DropAckHelper dropAckHelper;


    public Logger logger = LoggerFactory.getLogger(TblService.class);
    //@Override
    /*public void PushDataToHangYiXing() {
        String token = null;
        token = tokenHelper.GetToken();
        List<tblBpmEventsConfig> list = tblMapper.querytblBpmEventsConfig();
        int tblBPMEventPK = 402334964;
        int tblBPMEventPKnext = 402334964;
        int k = 0;
        for (int i = 0; i < 10; i++) {
            tblBPMEventPKnext = tblBPMEventPK + 10;
            List<Description> description = tblMapper.query(tblBPMEventPK, tblBPMEventPKnext);
            for (int L = 0; L < description.size(); L++)
            {
                String selement = description.get(L).getDescription();
                String[] selement1 = selement.split("\\.");
                for (int j = 0; j < selement1.length - 1; j++)
                {
                    selement1[j + 1] = pushDataPre.PreData(selement1[j + 1], list);
                }
                for (int n = 0; n < selement1.length; n++)
                {
                    String str3 = convertArrayToString(selement1);
                    String str4 = str3.replaceAll(",", "\n.");
                    String str5 = str4.replaceAll(" ", "");
                    int kk = str5.length() - 6;
                    String str6 = str4.substring(0, kk);
                    String str7 = str6 + "\n" + "ENDBPM";
                    description.get(L).setDescription(str7);
                }
            }
            tblBPMEventPK = tblBPMEventPK + 10;
            for (int j = 0; j < description.size(); j++) {
                String bpmResponse = pushDataHelper.PushData(token, description.get(j).getDescription());
                k = k + 1;
                System.out.println(description.get(j).getTblBPMEventPK() + "   BPM数据上传成功--" + bpmResponse + "第" + k + "条");
            }
        }
    }

    private String convertArrayToString(String[] strArr) {

        if (strArr == null || strArr.length == 0) {
            return "";
        }
        String res = "";
        for (int i = 0, len = strArr.length; i < len; i++) {
            res += strArr[i];
            if (i < len - 1) {
                res += ",";
            }
        }
        return res;
    }*/

    @Override

        public void DropDataToHangYiXing() throws InterruptedException {
        int P=0;

        String token = tokenHelper.GetToken();
        for (int i = 0; ; i++) {
            BpmRequest bpmResponse = dropDataHelper.DropData(token);
            if(bpmResponse.getData()==null){
                Thread.sleep(5000);
                continue;
            }
            if(bpmResponse.getData().getUuid()==null){
                Thread.sleep(5000);
                System.out.println("uuid为空");
                continue;
            }
            String bpmRequest  = dropAckHelper.DropAck( token, bpmResponse.getData().getUuid());
            System.out.println("上报成功"+ bpmRequest);

            P=P+1;
            System.out.println("第" + P + "条");
            Thread.sleep(1000);

            if (StringUtils.isEmpty(bpmResponse.getCode())) {
                System.out.println("data为空");
                continue;
            }

            if (StringUtils.isEmpty(bpmResponse.getMsg())) {
                System.out.println("data为空");
                continue;
            }
            if (StringUtils.isEmpty(bpmResponse.getData().getUuid())) {
                System.out.println("data为空");
                continue;
            }

            if (StringUtils.isEmpty(bpmResponse.getData().getMsgIds())||StringUtils.isEmpty(bpmResponse.getData().getMessage())) {
                System.out.println("data为空");
                continue;
            }

            List<Message> list = new ArrayList<>();
            for (int j = 0; j < bpmResponse.getData().getMessage().size(); j++) {
                Message message = new Message();
                message.setMsgIds(bpmResponse.getData().getMsgIds().get(j));
                message.setMessage(bpmResponse.getData().getMessage().get(j).replaceAll("\n", ""));
                list.add(j,message);

            }






            list=pushDataPre.removeDuplicate(list);
            for (int k = 0; k < list.size(); k++) {
                   List<tblBpmEventsFromHangYiXing> list1 = tblMapper.querymsgIds(list.get(k).getMsgIds());
                   if (list1.size() > 0) {
                       list.remove(list.get(k));
                       k=k-1;
                   }

                   continue;
               }
               if(list.size()>0){
               tblMapper.insert(list);

               System.out.println("筛选成功");
               }

            for (int j = 0; j <list.size() ; j++) {
                logger.info(list.get(j).getMsgIds()+list.get(j).getMessage());
            }

               }
            }


}

